<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\User as UserModel;

class User extends Controller
{
	public function userlist()
	{
		$list = UserModel::all(function($query){
			$query->where(['status'=>['neq',-1]])->order('id','asc');
		});

		$count = UserModel::where(['status'=>1])->count();
		$this->assign('count',$count);
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function userdel()
	{
		return $this->fetch();
	}

	public function userForbid($id)
	{
		$userModel = new UserModel();
		$res = $userModel->save(['status'=>0],['id'=>$id]);
		if ($res) {
			$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
		} 
		$this->result($_SERVER['HTTP_REFERER'], 0, '停用失败');
	}

	public function usershow()
	{
		$id = request()->param();
		$userinfo = UserModel::get($id);
		return $this->fetch('',[
				'userinfo' => $userinfo
			]);
	}

	/**
	 * 用户查找
	 * @return [type] [description]
	 */
	public function search()
	{
		$username = input('post.name');
		if (strpos($username,'@') !== false) {
			$user = model('User')->where('email',$username)->select();
		}else if(preg_match("/^1[34578]{1}\d{9}$/", $username)){
			$user = model('User')->where('mobile',$username)->select();
		} else{
			$where['username'] = ['like','%'.$username.'%'];
			$user = model('User')->where($where)->select();
		}
		// dump($user);exit;
		$count = count($user);
		return $this->fetch('user/userlist',[
				'list' => $user,
				'count' => $count
			]);
	}
}